package com.app.ytf.httpdemo.acvitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.app.ytf.httpdemo.R;
import com.app.ytf.httpdemo.retrofit.MovieService;
import com.app.ytf.httpdemo.retrofit.RetrofitEntrty;
import com.app.ytf.httpdemo.util.Constant;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author ytf
 */
public class SecondActivity extends BaseActivity {


    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.pro_bar)
    ProgressBar proBar;
    private String TAG = "SecondAcvitity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        getActivityIntent();
//        getDouBan();
        getDouBanByRetrofit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDouBanByRetrofit();
    }

    private void getDouBanByRetrofit() {
        proBar.setVisibility(View.VISIBLE);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.connectTimeout(360, TimeUnit.SECONDS)
//                .addInterceptor(interceptor)
                .writeTimeout(360, TimeUnit.SECONDS)
                .readTimeout(360, TimeUnit.SECONDS);
        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Constant.URL)
                .build();
        MovieService movieService = retrofit.create(MovieService.class);
        Observable<RetrofitEntrty> observable = movieService.getMovieList(0, 10);
        observable.subscribeOn(Schedulers.io())
//               .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RetrofitEntrty>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RetrofitEntrty retrofitEntrty) {
                        Logger.json(retrofitEntrty.toString());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    private void getActivityIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            String action = intent.getAction();
            String value1 = intent.getStringExtra("key1");
            String value2 = intent.getStringExtra("key2");
            Log.d(TAG, "Intent-action: " + action + "\nIntent-value1: " + value1 + "\nIntent-value1: " + value2);
        }
    }

    public static void actionIntent(Context context, String parmer1, String parmer2) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra("key1", parmer1);
        intent.putExtra("key2", parmer2);
        intent.setAction("abc");
        context.startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showMsg(RetrofitEntrty retrofitEntrty) {
        if (retrofitEntrty != null) {
            tvContent.setText(retrofitEntrty.getSubjects().toString());
            proBar.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
